package com.tjetc.common.task;

import com.tjetc.entity.Order;
import com.tjetc.service.AlipayService;
import com.tjetc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户端定时任务，用于定时查询订单，清理长时间未支付的订单
 * */

@Slf4j
@Component
public class AlipayTask {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AlipayService alipayService;


    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm(){
        log.info("orderConfirm 被执行......");

        List<Order> orderInfoList = orderService.getNoPayOrderByDuration(0);

        for (Order orderInfo : orderInfoList) {
            String orderNo = orderInfo.getNumber();
            log.info("核实订单 ===> {}", orderNo);
            //核实订单状态：调用微信支付查单接口
            alipayService.checkOrderStatus(orderInfo.getId());
        }
    }



}
